package com.tebiecloud.store.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.store.client.model.entity.StoreStaffFlow;
import com.tebiecloud.store.server.service.StoreStaffFlowService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 门店员工流水 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/storestaffflow")
public class StoreStaffFlowController {

    @Autowired
    private StoreStaffFlowService storeStaffFlowService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<IPage<StoreStaffFlow>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        StoreStaffFlow query = pageParams.mapToObject(StoreStaffFlow.class);
        QueryWrapper<StoreStaffFlow> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(storeStaffFlowService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<StoreStaffFlow> get(@RequestParam("id") Long id) {
        StoreStaffFlow entity = storeStaffFlowService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param amount 变化金额，单位分（负数为支出，正数为收入）,
     * @param type 流水类型，1订单核销收入2提现,
     * @param typeId 流水类型对应的id,
     * @param desp 描述,
     * @param year 年份,
     * @param month 月份,
     * @param day 日期
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "type", required = false) Integer type,
        @RequestParam(value = "typeId", required = false) Long typeId,
        @RequestParam(value = "desp", required = false) String desp,
        @RequestParam(value = "year", required = false) Integer year,
        @RequestParam(value = "month", required = false) Integer month,
        @RequestParam(value = "day", required = false) Integer day
    ) {
        StoreStaffFlow entity = new StoreStaffFlow();
        entity.setAmount(amount);
        entity.setType(type);
        entity.setTypeId(typeId);
        entity.setDesp(desp);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setDay(day);
        storeStaffFlowService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param amount 变化金额，单位分（负数为支出，正数为收入）
     * @param type 流水类型，1订单核销收入2提现
     * @param typeId 流水类型对应的id
     * @param desp 描述
     * @param year 年份
     * @param month 月份
     * @param day 日期
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "storeStaffId", required = false) Long storeStaffId,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "typeId", required = false) Long typeId,
            @RequestParam(value = "desp", required = false) String desp,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "day", required = false) Integer day
    ) {
        StoreStaffFlow entity = new StoreStaffFlow();
        entity.setStoreStaffId(storeStaffId);
        entity.setAmount(amount);
        entity.setType(type);
        entity.setTypeId(typeId);
        entity.setDesp(desp);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setDay(day);
        storeStaffFlowService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            storeStaffFlowService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            storeStaffFlowService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
